import * as THREE from 'three';

export default class BoundingCalculator {
  box3 = new THREE.Box3()
  size = new THREE.Vector3()
  center = new THREE.Vector3()
 
  // 主函数：计算包围盒
  calculate(object: THREE.Object3D) {
   this.box3.setFromObject(object)
   this.box3.getCenter(this.center)
   this.box3.getSize(this.size)
  }
 
  // 计算包围盒（从父节点剥离，并撤销自身变换）
  calculateWithoutTransform(object: THREE.Object3D) {
   const objectClone = object.clone() // 克隆就会从父节点剥离
   objectClone.position.set(0, 0, 0)
   objectClone.rotation.set(0, 0, 0, THREE.Euler.DEFAULT_ORDER)
   objectClone.scale.set(1, 1, 1)
   this.calculate(objectClone)
  }
 
  // 输出到包围盒物体
  writeToBoxObject(object: THREE.Object3D) {
   object.position.copy(this.center)
   object.scale.copy(this.size)
  }
 
  // 输出结果（中心、大小）
  writeCenterAndSize(center: THREE.Vector3, size: THREE.Vector3) {
   center.copy(this.center)
   size.copy(this.size)
  }
 
  // 输出中心
  writeCenter(center: THREE.Vector3) {
   center.copy(this.center)
  }
 
  // 输出大小
  writeSize(size: THREE.Vector3) {
   size.copy(this.size)
  }
 }
 
 // 全局唯一的单例
 export const boundingCalculator = new BoundingCalculator()